/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PhucAnh
 */
public class ArrowDown extends Shape{
    Point pointStart;
    Point pointFinish;
    
    int xa,ya,xb,yb,xc,yc,xd,yd,xe,ye,xf,yf,xg,yg,xtemp,ytemp;
   
    int listX[] = new int[8];
    int listY[] = new int[8];
    
    public ArrowDown() {

    }

    public ArrowDown(Point pointStart, Point pointFinish, Color color, int basicStrok) {
        super(color, basicStrok);
        this.pointStart = pointStart;
        this.pointFinish = pointFinish;
        
        xa = pointStart.getX();
        ya = (pointStart.getY() + pointFinish.getY())/2;
        
        xtemp = (pointStart.getX() + pointFinish.getX())/2;
        ytemp = pointStart.getY();
                
        xb = (pointStart.getX() + xtemp)/2;
        yb = ya;
        
        xc = xb;
        yc = ytemp;
        
        xd = (pointFinish.getX() + xtemp)/2;
        yd = yc;
        
        xe = xd;
        ye = ya;
        
        xf = pointFinish.getX();
        yf = ya;
        
        xg = xtemp;
        yg = pointFinish.getY();
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xd;
        listX[4]=xe;
        listX[5]=xf;
        listX[6]=xg;
        listX[7]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=yd;
        listY[4]=ye;
        listY[5]=yf;
        listY[6]=yg;
        listY[7]=ya;
    }
    public void setPoint2(Point pointFinish){
        this.pointFinish = pointFinish;
        xa = pointStart.getX();
        ya = (pointStart.getY() + pointFinish.getY())/2;
        
        xtemp = (pointStart.getX() + pointFinish.getX())/2;
        ytemp = pointStart.getY();
                
        xb = (pointStart.getX() + xtemp)/2;
        yb = ya;
        
        xc = xb;
        yc = ytemp;
        
        xd = (pointFinish.getX() + xtemp)/2;
        yd = yc;
        
        xe = xd;
        ye = ya;
        
        xf = pointFinish.getX();
        yf = ya;
        
        xg = xtemp;
        yg = pointFinish.getY();
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xd;
        listX[4]=xe;
        listX[5]=xf;
        listX[6]=xg;
        listX[7]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=yd;
        listY[4]=ye;
        listY[5]=yf;
        listY[6]=yg;
        listY[7]=ya;
    }
    @Override
    public void drawShape(Graphics g2,BufferedImage img)
    {
        Point pointA = new Point(xa, ya);
        Point pointB = new Point(xb, yb);
        Point pointC = new Point(xc, yc);
        Point pointD = new Point(xd, yd);
        Point pointE = new Point(xe, ye);
        Point pointF = new Point(xf, yf);
        Point pointG = new Point(xg, yg);
        
        Graphics2D g= (Graphics2D)g2;
        g.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        g.drawPolygon(listX, listY, 8);
    }
    @Override
    public void toMove(int m,int n)
    {
//        for(int i=0;i<8;i++){
//            listX[i]+=m;
//            listY[i]+=n;
//        }
        
        pointFinish.toMove(m, n);
        pointStart.toMove(m, n);
        setPoint2(pointFinish);
    }
    
    public boolean isContain(Point p){
        if(pointFinish.getX()>=pointStart.getX() && pointFinish.getY()>=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()<pointStart.getX() && pointFinish.getY()<pointStart.getY()){
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()>=pointStart.getX() && pointFinish.getY()<=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else {
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        }
    }
    
    public void replay(Graphics g,int time,Image a,Control.PlayBack.MyPanel1 a2){
        int x=pointFinish.getX()- pointStart.getX();
        int y=pointFinish.getY()- pointStart.getY();
        Graphics2D g2D= (Graphics2D)g;
        g2D.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        Image img = a;
        Point tmp=new Point();
        for(int i=0;i<=time;i++){
          g.drawImage(img,0, 0, a2);
          tmp.setX(x*i/time+pointStart.getX());
          tmp.setY(y*i/time+pointStart.getY());
          setPoint2(tmp);
          g.drawPolygon(listX, listY, 8);
          try {
                Thread.sleep(time);
              } catch (InterruptedException ex) {
          Logger.getLogger(Curve.class.getName()).log(Level.SEVERE, null, ex);
              }
        }
    }
    
}
